import type { ScriptManifest } from "../../declarations/script-manifest.js";
import { debug, AdRules, alertLog } from "./buildConfig.mjs";
import presets from "./_preset.mjs";

const manifest: ScriptManifest = {
  name: "AdBlock Script for WebView",
  "name:zh-CN": "套壳油猴的广告拦截脚本",
  author: "Lemon399",
  version: "2.8.7",
  description: "Parse ABP Cosmetic rules to CSS and apply it.",
  "description:zh-CN": "将 ABP 中的元素隐藏规则转换为 CSS 使用",
  resource: {},
  match: ["http://*/*", "https://*/*"],
  "run-at": "document-start",
  grant: null,
  namespace: "https://lemon399-bitbucket-io.vercel.app/",
  source: [
    "https://gitee.com/lemon399/tampermonkey-cli/tree/master/projects/abp_parse",
    "https://bitbucket.org/lemon399/tampermonkey-cli/src/master/projects/abp_parse/",
  ],
  connect: [],
  copyright: "GPL-3.0",
  license: "GPL-3.0",
};

for (const rule of presets.onlineRules) {
  const domain = /:\/\/([\w.-]+?)\//.exec(rule.地址)?.[1];
  if (manifest.resource) {
    manifest.resource[rule.标识] = rule.地址;
  }
  if (
    domain &&
    Array.isArray(manifest.connect) &&
    !manifest.connect.includes(domain)
  ) {
    manifest.connect.push(domain);
  }
}

if (debug) {
  manifest.name += " Debug";
  (manifest["name:zh-CN"] as string) += " 调试";
  manifest.version += "c";
}

if (AdRules) {
  manifest.version += "r";
}

if (alertLog) {
  manifest.version += "a";
}

export default manifest;
